﻿using System;
using SdmxMl.Helper;
using SdmxMl.Structure;
using SdmxMl.Manager;
using SdmxMl.Common;

using System.Windows.Forms;

namespace SdmxMl.UC
{
    public partial class UcCategory : UcBase
    {
        private Category ct = null;
        private bool _isFinal;

        public UcCategory()
        {
            InitializeComponent();
            customComboBoxName.DropDownControl = ucMlText;
        }

        public override bool BindFromNode(TreeNode node, bool isFinal)
        {
            _isFinal = isFinal;
            ct = null;
            base.BindFromNode(node, isFinal);
            if (node != null)
                ct = node.Tag as Category;
            BindToControls();

            return Visible;
        }

        // No edition if Parent Artefact IsFinal
        private void IsFinalEditControls(bool isFinal)
        {
            bool enabled = isFinal == false;

            textBoxId.ReadOnly = isFinal;
            textBoxVersion.ReadOnly = isFinal;
            textBoxUrn.ReadOnly = isFinal;
            textBoxUri.ReadOnly = isFinal;
            customComboBoxName.IsFinal = isFinal;
            comboMultiLinesDescr.IsFinal = isFinal;
            checkBoxIsFinal.Enabled = enabled;

            ucFlowRefListMeta.IsFinal = isFinal;
            ucFlowRefListData.IsFinal = isFinal;

        }

        private void BindToControls()
        {
            buttonNavigateUri.Visible = false;
            if (ct != null)
            {
                Visible = true;
                textBoxId.Text = ct.Id;
                textBoxVersion.Text = ct.Version;
                textBoxUrn.Text = ct.Urn;
                textBoxUri.Text = ct.Uri;
               if (string.IsNullOrEmpty(ct.Uri) == false)
                    buttonNavigateUri.Visible = true;
                customComboBoxName.Text = ct.Name.ToString();
                ucMlText.ML = ct.Name;
                comboMultiLinesDescr.Text = ct.Description.ToString();
                comboMultiLinesDescr.ML = ct.Description;
                ucFlowRefListMeta.Table = ct.MetaDataFlowRefList.AsDataTable();
                ucFlowRefListMeta.ArtefactType = SdmxArtefactType.Metadataflows;
                ucFlowRefListData.Table = ct.DataFlowRefList.AsDataTable(); ;
                ucFlowRefListData.ArtefactType = SdmxArtefactType.Dataflows;
                checkBoxIsFinal.Checked = ct.IsExternalReference;
                IsFinalEditControls(_isFinal);
                FillParentInfo(textBoxParent, textBoxParentCodeDescr);

                button1.Visible = false; // ct.AnnotationList.GetTextOfAnnotationType("SMS_TYPE").Length > 0;

            }
            else
            {
                Visible = false;
            }
        }

        protected override bool BindFromControls()
        {
            bool err = false;
            if (ct != null)
            {
                // Update tree img if needed
                int imgIndex = ct.UsedImageIndex;
                if (imgIndex != treeNode.ImageIndex)
                    treeNode.ImageIndex = treeNode.SelectedImageIndex =imgIndex;

                CategoryList cal = ct.InnerHierarchy as CategoryList;
                err = CheckUniqueId(textBoxId, cal.GetHierarchyManageOfItem(ct));
              //  ct.Id = textBoxId.Text;
                ct.Version = textBoxVersion.Text;
                ct.Urn = textBoxUrn.Text;
                ct.Uri = textBoxUri.Text;
                ct.Name.UpdateForCurrentLanguage(customComboBoxName.Text);
                ct.Description.UpdateForCurrentLanguage(comboMultiLinesDescr.Text);

                ct.MetaDataFlowRefList.FromDataTable(ucFlowRefListMeta.Table);
                ct.DataFlowRefList.FromDataTable(ucFlowRefListData.Table);                
            }
            return err;
        }

        private void textBoxValue_Leave(object sender, EventArgs e)
        {
            if (ct != null)
            {
                CategoryList cal = ct.InnerHierarchy as CategoryList;
                if (cal != null)
                    CheckUniqueId(textBoxId, cal.GetHierarchyManageOfItem(ct));
            }
        }

        /// <summary>
        /// Dummy for Activity OFS
        /// </summary>
        /// <param name="sender"></param> <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            // get Full Id,
            string id = ct.FullId;

            TreeView tv = treeNode.TreeView;
            TreeNode targetNode = null;
            if (tv != null)
            {
                TreeHelper.LocateText(tv.Nodes[tv.Nodes.Count - 1], id, ref targetNode);
                if (targetNode != null)
                    tv.SelectedNode = targetNode;
            }
        }

        private void panelFlow_Resize(object sender, EventArgs e)
        {
            int middle = this.panelMiddle.Width;
            int width = (this.Width / 2) - middle / 2;
            this.panelLeft.Width = width;
            this.panelRight.Width = width;

        }

        private void buttonRegistry_Click(object sender, EventArgs e)
        {
            try
            {
                using (new SdmxMl.Helper.CWaitCursor())
                {
                    // If URN try to get it if not already in memory
                    ArtefactRefTyped art = UrnManager.UrnToArtefactRefTyped(ct.Uri);
                    if (art != null)
                    {
                        UrnParser p = new UrnParser(ct.Uri);

                        // If already in memory, navigate otherwhile get from regidtry
                        if (NavigationManager.LocatenavigateProject(art.Type, art.FullIdent, p) == null)
                            NavigationManager.NavigateWithRegistry(art.Type, art.FullIdent, true, p);
                    }
                    else
                    {
                        // Try to process document
                        System.Diagnostics.Process myProc;
                        myProc = System.Diagnostics.Process.Start(ct.Uri);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void textBoxUri_TextChanged(object sender, EventArgs e)
        {
            // External Navigation Command
            ct.Uri = textBoxUri.Text;
            buttonNavigateUri.Visible = textBoxUri.Text.Length > 0;
            ArtefactRefTyped art = UrnManager.UrnToArtefactRefTyped(ct.Uri);
            if (art != null)
                textBoxUri.BackColor = System.Drawing.Color.LightYellow;
            else
                textBoxUri.BackColor = textBoxUrn.BackColor;


        }

    }
}
